package drds.common.db.sql_mapping.dynamic_tag.sql_template;

import drds.common.db.sql_mapping.configuration.Configuration;
import drds.common.db.sql_mapping.dynamic_tag.tag.Tag;
import drds.common.db.sql_mapping.dynamic_tag.tag_context.TagContext;

import java.util.Map;


public class DynamicSqlTemplate extends AbstractSqlTemplate {

    private final Tag rootTag;

    public DynamicSqlTemplate(Configuration configuration, Tag rootTag) {
        super(configuration);
        this.rootTag = rootTag;
    }

    @Override
    public Bind bindWith(Map<String, Object> parameterObject) {
        TagContext tagContext = new TagContext(parameterObject);
        rootTag.apply(tagContext);
        SqlTemplate sqlTemplate = parse(tagContext.getSql());
        Bind bind = sqlTemplate.bindWith(parameterObject);
        //补足bind参数
        parameterObject.putAll(tagContext.getTagContextMap());
        //
        return bind;
    }
}
